use anyhow::Context;
use std::process::Command;

#[derive(serde::Deserialize)]
pub struct SymbolInfo {
    pub feed_native: String,
    pub order_native: String,
    pub relative_norm: String,
    pub absolute_norm: String,
}

pub async fn from_norm(mea: &str, symbol: &str) -> anyhow::Result<SymbolInfo> {
    let output = Command::new("./bazel-bin/cc/appcoin2/support/symbology/symbol-convert")
        .args(&[mea, "--norm", symbol])
        .current_dir("../..")
        .output()
        .with_context(|| {
            format!("Do you have ./bazel-bin/cc/appcoin2/support/symbology/symbol-convert ?")
        })?;
    if output.status.success() {
        let stdout =
            std::str::from_utf8(&output.stdout).context("symbol-convert stdout is not utf8")?;
        Ok(toml::from_str::<SymbolInfo>(&stdout)?)
    } else {
        let stderr =
            std::str::from_utf8(&output.stderr).context("symbol-convert stderr is not utf8")?;
        Err(anyhow::anyhow!("symbol-convert returns error: {}", stderr))
    }
}
